package com.zhilianbao.leyaogo.ui.fragment.me.myaccount;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.bql.recyclerview.swipe.SwipeMenuAdapter;
import com.bql.recyclerview.swipe.SwipeMenuCreator;
import com.bql.utils.EventManager;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.events.LeEventCode;
import com.zhilianbao.leyaogo.http.api.AccountApi;
import com.zhilianbao.leyaogo.http.callback.LoadingViewCallback;
import com.zhilianbao.leyaogo.model.response.me.CouponResponse;
import com.zhilianbao.leyaogo.ui.adapter.me.CouponAdapter;
import com.zhilianbao.leyaogo.ui.fragment.base.SwipeRefreshAndLoadFragment;
import com.zhilianbao.leyaogo.utils.Utils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import okhttp3.Response;

/**
 * Created by liu on 2016/10/26.
 * 优惠券
 */
public class CouponFragment extends SwipeRefreshAndLoadFragment<CouponResponse> {
    CouponAdapter mCouponAdapter;

    public static CouponFragment getInstance() {
        CouponFragment fragment = new CouponFragment();
        return fragment;
    }

    @Override
    public void onRcvItemClick(RecyclerView.ViewHolder holder, int position) {
//        test
//        Intent intent = new Intent(mActivity,SeeAvalibleGoodsActivity.class);
//        intent.putExtra("couponsId",mList.get(position).getCouponsId());
//        intent.putExtra("couponsLimitPrice",mList.get(position).getCouponsLimitPrice());
//        mActivity.startActivity(intent);
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_coupon;
    }

    @Override
    public SwipeMenuAdapter getAdapter() {
        return mCouponAdapter;
    }

    @Override
    public RecyclerView.ItemDecoration getItemDecoration() {
        return null;
    }

    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(mActivity);
    }

    @Override
    public SwipeMenuCreator getSwipeMenuCreator() {
        return null;
    }

    @Override
    public void onLeftSwipeMenuClick(int adapterPosition, int menuPosition) {

    }

    @Override
    public void onRightSwipeMenuClick(int adapterPosition, int menuPosition) {

    }


    @Override
    public void onRefresh() {
        loadDataList(1, true);
    }

    @Override
    public void loadDataList(int curPage, boolean isPullToRefresh) {
        mCurPage = curPage;
        AccountApi.getMyCouponList(mActivity, Utils.getUserLoginInfo().getUserId(),Utils.getSupplierId(),mCurPage,pageSize, new RefreshAndLoadCallback<List<CouponResponse>>(isPullToRefresh) {
            @Override
            public void errorLeftOrEmptyBtnClick(View v) {
                TextView tv = (TextView) v;
                if (tv.getText().toString().trim().equals(emptyBtnText())) {
                    EventBus.getDefault().post(new EventManager(LeEventCode.MSG_CODE_SWITCH_TO_HOME));
                    mActivity.finish();
                } else {
                    loadDataList(1, false);
                }
            }

            @Override
            public void onResultSuccess(List<CouponResponse> couponResponses, @Nullable Response response, LoadingViewCallback callback) {
//                mList.addAll(couponResponses);
                handleRefreshAndLoadListData(mCurPage,callback, couponResponses);
            }

            @Override
            public Drawable emptyDrawable() {
                return getResources().getDrawable(R.drawable.img_coupons_for_empty);
            }

            @Override
            public String emptyContent() {
                return getString(R.string.empty_coupon);
            }

            @Override
            public boolean showEmptyButton() {
                return true;
            }

            @Override
            public String emptyBtnText() {
                return getString(R.string.empty_go);
            }
        });
    }

    @Override
    public void onLoadMore() {
        loadDataList(mCurPage, true);
    }

    @Override
    protected void initToolbarHere() {
        initToolbar(getString(R.string.coupon));
    }

    @Override
    protected void initViewsAndEvents(Bundle savedInstanceState) {
        mCouponAdapter = new CouponAdapter(mActivity, mList);
    }

    @Override
    public boolean registerEventBus() {
        return true;
    }

    @Override
    protected void onHandleEvent(EventManager eventManager) {
        super.onHandleEvent(eventManager);
        switch (eventManager.getEventCode()) {
            case LeEventCode.MSG_CODE_FINISH_GOODS_ACTIVITY://从查看适用商品进入购物车,点击去逛逛,finish掉优惠券页面
                mActivity.finish();
                break;
        }
    }
}
